package com.efast.dmom.mes.datahandlers.entity;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.dmom.mes.bean.BomItem;
import com.efast.dmom.mes.bean.Part;
import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.dao.entity.BomItemDao;
import com.efast.dmom.mes.datahandlers.base.PartRelateDataHandler;
import com.efast.dmom.mes.entity.BomItemEntity;
import com.efast.dmom.mes.util.TimeUtil;

@Component
public class BomItemDataHandler extends PartRelateDataHandler<BomItem>{
	@Autowired
	private BomItemDao bomItemDao;
	@Autowired
	private PartDataHandler partHandler;
	
	@Override
	public ResultBean save(BomItem obj) {
		ResultBean resultBean = new ResultBean();
		if (obj.getInnerData() != null) {
			obj.getInnerData().setSiteId(DataHandlerUtils.getSiteId());
			obj.getInnerData().setCreationTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setCreationTimeZone(TimeUtil.getLocalTimeZone());
			obj.getInnerData().setCreator(SpringWebUtils.getRequestUser());
			obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
			obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
//			obj.getInnerData().setCreationPid(DataHandlerUtils.getDmomPid());
//			obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());
			obj.getInnerData().setRowStatus(1);
			obj.getInnerData().setRowTrxId(1);
			bomItemDao.save(obj.getInnerData());
		}
		return resultBean;
	}
	@Override
	public ResultBean update(BomItem obj) {
		ResultBean resultBean = new ResultBean();
		if (obj.getInnerData() != null) {
			obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
			obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
//			obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());		
			bomItemDao.update(obj.getInnerData());
		}
		return resultBean;
	}
	@Override
	public ResultBean delete(BomItem obj) {
		ResultBean resultBean = new ResultBean();
		if (obj.getInnerData() != null) {
			obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
			obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
//			obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());		
			bomItemDao.delete(obj.getInnerData());
		}
		return resultBean;
	}

	public BomItem getById(long id) {
		BomItem bomItem = new BomItem();
		bomItem.setInnerData(bomItemDao.getById(id));
		return bomItem;
	}

	public BomItem getByName(String name) {
		BomItem bomItem = new BomItem();
		bomItem.setInnerData(bomItemDao.getByName(name));
		return bomItem;
	}
	
	//delete a completed bom,to delete sub-level bomItem
	public void deleteByBom(BomItem obj) {
		if (obj.getInnerData() != null) {
			obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
			obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
//			obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());		
			bomItemDao.deleteByBom(obj.getInnerData());
		}
	}
	
	public List<BomItem> getByBomId(long bomId){
		
		List<BomItem> itemList = new ArrayList<BomItem>();
		
		List<BomItemEntity> entityList = bomItemDao.getByBomId(bomId);
		
		if (entityList != null && entityList.size() > 0) {
			for(BomItemEntity entity : entityList){
				BomItem bomItem = new BomItem();
				bomItem.setInnerData(entity);
				Part part = partHandler.getPartByPartNo(entity.getPartNo(), entity.getPartRevision());
				if(part !=null) {
					bomItem.setPart(part);
				}
				itemList.add(bomItem);
			}
			return itemList;
		}
		return null;
	}
	public int getCountByPartAndRevision(String partNo,String partRevision) {
		return bomItemDao.getCountByPartAndRevision(partNo, partRevision);
	}

	public BomItem getBomItemByPartNoAndRevision(BomItem bomItem) {
		BomItem bomItemData = new BomItem();
		bomItemData.setInnerData(bomItemDao.getBomItemByPartNoAndRevision(bomItem.getInnerData()));
		return bomItemData;
	}

	public List<BomItemEntity> getBomItemByBomId(BomItem bomItemData,int start ,int length) {
		
		List<BomItemEntity> entityList = bomItemDao.getBomItemByBomId(bomItemData.getInnerData().getBomId(),start,length);
		
		if (entityList != null && entityList.size() > 0) {
			return entityList;
		}
		return null;
	}

	@Override
	public ResultBean saveBatch(List<BomItem> obj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ResultBean updateBatch(List<BomItem> obj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ResultBean deleteBatch(List<BomItem> obj) {
		// TODO Auto-generated method stub
		return null;
	}
}
